<?php


/**
 * @param $pwd 管理员登录密码
 * @param $phone 管理员账号
 * @return string
 */
function add_log($content){
    $str = array('aid' => $_SESSION['id'], 'content' =>$content, 'ctime' => time());
    M('admin_log', '', 'KEFU')->add($str);
}

/**
 * @param $pwd 管理员登录密码
 * @param $phone 管理员账号
 * @return string
 */
function set_pwd($pwd,$phone){
    return md5($pwd.'dxd'.$phone);
}
/**
 * 助通短信平台
 * @param string $mobile 手机号
 * @param string $content 内容
 */
function send_sms($mobile, $content)
{
    date_default_timezone_set('PRC');//设置时区
    $url = 'http://www.api.zthysms.com/sendSms.do';
    $data['username'] = 'jiangyuyunhy';
    $data['tkey'] = date('YmdHis');
    $data['password'] = md5(md5('eMV5qU') . $data['tkey']);
    $data['mobile'] = $mobile;
    $data['content'] = $content;
    return set_curl($url, http_build_query($data));
}

/**
 * @param string $url 上传地址
 * @param array $data 上传数组
 * @param string $header 头文件设置
 */
function set_curl($url, $data = array(), $header = '')
{
    $ch = curl_init();
    if (!empty($header)) {
//            $header=array('Content-type: application/json','charset:utf-8'); 示例
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, '1');
    curl_setopt($ch, CURLOPT_URL, $url);
    if (!empty($data)) {
        curl_setopt($ch, CURLOPT_POST, '1');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    //禁用后cURL将终止从服务端进行验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $res = curl_exec($ch);
    if (curl_error($ch)) {
        return '错误信息:' . curl_error($ch);
    }
    curl_close($ch);
    return $res;
}




//获取IP 地址
function getIP(){
    if (isset($_SERVER)) {
        if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
            $IPaddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
        } else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
            $IPaddress = $_SERVER["HTTP_CLIENT_IP"];
        } else {
            $IPaddress = $_SERVER["REMOTE_ADDR"];
        }
    } else {
        if (getenv("HTTP_X_FORWARDED_FOR")) {
            $IPaddress = getenv("HTTP_X_FORWARDED_FOR");
        } else if (getenv("HTTP_CLIENT_IP")) {
            $IPaddress = getenv("HTTP_CLIENT_IP");
        } else {
            $IPaddress = getenv("REMOTE_ADDR");
        }
    }
    return $IPaddress;
}


/**
 * 添加管理员登录日志
 * @param $data管理员id
 */
function addlog($aid, $content='')
{
    $logmodel = M('admin_log', '', 'KEFU');
    if (empty($content)) {
        $content = "在ip为:" . getIP() . "登录。";
    }
    $data = [
        'aid' => $aid,
        'content' => $content,
        'ctime' => time()
    ];
    $logmodel->add($data);
}


/**
 * 添加推送/发送信息日志
 * @param string $vid 用户唯一标示
 * @param $content 推送/发送内容
 * @param string $title 推送/发送标题
 * @param string $type 类型，1个人，2群体
 */
function add_news($vid='0', $content, $type='1')
{
    $info = M('user_news','','USER');
    $data['vid'] = $vid;
    $data['title'] = '系统信息';
    $data['content'] = $content;
    $data['ctime'] = time();
    $data['type'] = $type;
    $info->add($data);
}

/**
 * 记录额度变动
 * @param $vid 用户标识
 * @param $content 修改理由
 * @param $money 变动金额
 */
function add_quota_record($vid,$content,$money,$num){
    $info=M('user_quota_record','','CAIWU');
    $data['vid']=$vid;
    $data['content']=$content;
    $data['money']=$money;
    $data['type']=2;
    $data['admin_name']=$_SESSION['name'];
    $data['is_first'] = $num+1;
    $data['ctime']=time();
    $info->add($data);

}

/**
 * 递归获取无限级分类
 */
function get_new_array($data = array(), $pre_openid = '', $arrs = array())
{
    static $arr = array();
    if (!empty($arrs)) {
        for ($i = 0; $i < count($arrs); $i++) {
            array_shift($arr);
        }
    }else{
        $pre_openid = empty($pre_openid) ? '0' : $pre_openid;
        foreach ($data as $key => $value) {
            if ($value['pre_openid'] == $pre_openid) {
                $arr[] = $value;
                get_new_array($data, $value['openid']);
            }
        }
        return $arr;
    }

}

/**
 * 获取该用户的所有上级
 */
//static $a;
function get_all_pre($data = array(), $openid = '')
{

    static $parr;
    foreach ($data as $key => $value) {
        if ($value['openid'] == $openid) {
            $parr[] = $value;
            get_all_pre($data, $value['pre_openid']);
        }
    }
    return $parr;
}


/**
 * 递归获取无限级分类(后台权限添加使用)
 */
function get_new_arrays($data = array(), $pid = '', $arrs = array())
{
    static $arrss = array();
    if (!empty($arrs)) {
        for ($i = 0; $i < count($arrs); $i++) {
            array_shift($arrss);
        }
    }
    $pid = empty($pid) ? '0' : $pid;
    foreach ($data as $key => $value) {
        if ($value['pid'] == $pid) {
            $arrss[] = $value;
            get_new_arrays($data, $value['id']);
        }
    }
    return $arrss;
}



/**
 * 获取access_token
 * @return mixed
 */
function getaccesstoken()
{
    $appid = 'wx316eb0c609ad7e3a';
    $appsecret = '7e5b25fb7a42ead7f6fc469148c20a97';
    $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
    $res = getcurl($url);
    $resobj = json_decode($res);
    $_SESSION['access_token'] = $resobj->access_token;
    $_SESSION['expires_time'] = time() + 3600;
    return $_SESSION['access_token'];
}

/**
 * @param $url
 * @param string $type 默认get，可填post
 * @param string $arr $type为post时需要传输的数组
 * @return 返回json数据
 */
function getcurl($url = '', $type = 'get', $arr = '')
{
    $ch = curl_init();
    //设置1表示存入变量，设置0的话表示直接输出
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    if ($type == 'post') {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
    }
    //禁用后cURL将终止从服务端进行验证
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    $res = curl_exec($ch);
    if (curl_errno($ch)) {
        return '错误' . curl_errno($ch);
    }
    curl_close($ch);
    return $res;
}
//测试程序执行时间使用
function get_microtime()
{
    $time = explode(' ', microtime());
    return $time['1'] + $time['0'];
}

function removexss($val){
    static $obj=null;
    if($obj===null){
        include('./Public/htmlpurifier/HTMLPurifier.includes.php');
        $config=HTMLpurifier_Config::createDefault();
        $config->set('HTML.TargetBlank',TRUE);
        $obj=new HTMLPurifier($config);
    }
    return $obj->purify($val);
}


function role($rid){
    $where['status']=1;
    $where['pid']=$rid;
    $last_id=M('role', '', 'KEFU')->where(['id'=>$_SESSION['role_id']])->getfield('last_id');
    $where['id']=['in',$last_id];
    return M('power', '', 'KEFU')->WHERE($where)->order('weight asc')->SELECT();
}